﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Sprites
{
	public class Mushroom : Sprite
	{
		private static double GROUND_INERTIA = 0.89f;
		private static double AIR_INERTIA = 0.89f;

		private double runTime;
		private bool onGround = false;
		private bool mayJump = false;
		//private int jumpTime = 0;
		//private double xJumpSpeed;
		//private double yJumpSpeed;

		private int width = 4;
		int height = 24;

		private LevelScene world;
		public int facing;

		public bool avoidCliffs = false;
		private int life;

		public Mushroom(LevelScene world, int x, int y)
		{
			sheet = Art.items;

			this.x = x;
			this.y = y;
			this.world = world;
			xPicO = 8;
			yPicO = 15;

			yPic = 0;
			height = 12;
			facing = 1;
			wPic = hPic = 16;
			life = 0;
		}

		public override void collideCheck()
		{
			double xMarioD = world.mario.x - x;
			double yMarioD = world.mario.y - y;
			//double w = 16;
			if (xMarioD > -16 && xMarioD < 16)
			{
				if (yMarioD > -height && yMarioD < world.mario.height)
				{
					world.mario.getMushroom();
					spriteContext.removeSprite(this);
				}
			}
		}

		public override void move()
		{
			if (life < 9)
			{
				layer = 0;
				y--;
				life++;
				return;
			}
			double sideWaysSpeed = 1.75f;
			layer = 1;
			//        double sideWaysSpeed = onGround ? 2.5f : 1.2f;

			if (xDelta > 2)
			{
				facing = 1;
			}
			if (xDelta < -2)
			{
				facing = -1;
			}

			xDelta = facing * sideWaysSpeed;

			mayJump = (onGround);

			xFlipPic = facing == -1;

			runTime += (Math.Abs(xDelta)) + 5;



			if (!move(xDelta, 0)) facing = -facing;
			onGround = false;
			move(0, yDelta);

			yDelta *= 0.85f;
			if (onGround)
			{
				xDelta *= GROUND_INERTIA;
			}
			else
			{
				xDelta *= AIR_INERTIA;
			}

			if (!onGround)
			{
				yDelta += 2;
			}
		}

		private bool move(double xa, double ya)
		{
			while (xa > 8)
			{
				if (!move(8, 0)) return false;
				xa -= 8;
			}
			while (xa < -8)
			{
				if (!move(-8, 0)) return false;
				xa += 8;
			}
			while (ya > 8)
			{
				if (!move(0, 8)) return false;
				ya -= 8;
			}
			while (ya < -8)
			{
				if (!move(0, -8)) return false;
				ya += 8;
			}

			bool collide = false;
			if (ya > 0)
			{
				if (isBlocking(x + xa - width, y + ya, xa, 0)) collide = true;
				else if (isBlocking(x + xa + width, y + ya, xa, 0)) collide = true;
				else if (isBlocking(x + xa - width, y + ya + 1, xa, ya)) collide = true;
				else if (isBlocking(x + xa + width, y + ya + 1, xa, ya)) collide = true;
			}
			if (ya < 0)
			{
				if (isBlocking(x + xa, y + ya - height, xa, ya)) collide = true;
				else if (collide || isBlocking(x + xa - width, y + ya - height, xa, ya)) collide = true;
				else if (collide || isBlocking(x + xa + width, y + ya - height, xa, ya)) collide = true;
			}
			if (xa > 0)
			{
				if (isBlocking(x + xa + width, y + ya - height, xa, ya)) collide = true;
				if (isBlocking(x + xa + width, y + ya - height / 2, xa, ya)) collide = true;
				if (isBlocking(x + xa + width, y + ya, xa, ya)) collide = true;

				if (avoidCliffs && onGround && !world.level.isBlocking((int)((x + xa + width) / 16), (int)((y) / 16 + 1), xa, 1)) collide = true;
			}
			if (xa < 0)
			{
				if (isBlocking(x + xa - width, y + ya - height, xa, ya)) collide = true;
				if (isBlocking(x + xa - width, y + ya - height / 2, xa, ya)) collide = true;
				if (isBlocking(x + xa - width, y + ya, xa, ya)) collide = true;

				if (avoidCliffs && onGround && !world.level.isBlocking((int)((x + xa - width) / 16), (int)((y) / 16 + 1), xa, 1)) collide = true;
			}

			if (collide)
			{
				if (xa < 0)
				{
					x = (int)((x - width) / 16) * 16 + width;
					this.xDelta = 0;
				}
				if (xa > 0)
				{
					x = (int)((x + width) / 16 + 1) * 16 - width - 1;
					this.xDelta = 0;
				}
				if (ya < 0)
				{
					y = (int)((y - height) / 16) * 16 + height;
					//jumpTime = 0;
					this.yDelta = 0;
				}
				if (ya > 0)
				{
					y = (int)(y / 16 + 1) * 16 - 1;
					onGround = true;
				}
				return false;
			}
			else
			{
				x += xa;
				y += ya;
				return true;
			}
		}

		private bool isBlocking(double _x, double _y, double xa, double ya)
		{
			int x = (int)(_x / 16);
			int y = (int)(_y / 16);
			if (x == (int)(this.x / 16) && y == (int)(this.y / 16)) return false;

			bool blocking = world.level.isBlocking(x, y, xa, ya);

			byte block = world.level.getBlock(x, y);

			return blocking;
		}

		public override void bumpCheck(int xTile, int yTile)
		{
			if (x + width > xTile * 16 && x - width < xTile * 16 + 16 && yTile == (int)((y - 1) / 16))
			{
				facing = -world.mario.facing;
				yDelta = -10;
			}
		}

	}
}